package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class BinaryMap {

	private File file;
	private Scanner scanner;
	private Dimension mapDimension;

	public BinaryMap(String pathFile) {
		file = new File(pathFile);

		mapDimension = checkMapDimension();
		System.out.println(mapDimension.toString());
		this.createMap();
	}

	private Dimension checkMapDimension() {
		int numLines = 0;
		String line = "";
		try {
			scanner = new Scanner(file);
		} catch (FileNotFoundException exceptionFileNotFound) {
			exceptionFileNotFound.printStackTrace();
		}
		while (scanner.hasNextLine()) {
			line = scanner.nextLine();
			numLines++;
		}
		scanner.close();

		Dimension d = new Dimension(numLines, line.length());
		return d;
	}

	public int[][] createBinaryMap() {
		int new_dimy = (mapDimension.dimY+1)/2;
		int[][] new_map = new int[mapDimension.dimX][new_dimy];
		int[][] map = createMap();
		boolean sequence3_0 = false;
		int count = 0;
		for (int i = 0; i < mapDimension.dimX; i++) {
			count = 0;
			sequence3_0 = false;
			for (int j = 0; j < mapDimension.dimY; j++) {
				if (j < (mapDimension.dimY - 2)) {
					if (map[i][j] == 9) {
						new_map[i][count] = 9;
						sequence3_0 = false;
					} else if (map[i][j] == 1 && map[i][j + 1] == 1
							&& map[i][j + 2] == 1) {
						j = j + 2;
						new_map[i][count] = 1;
					} else if (map[i][j] == 0 && map[i][j + 1] == 0
							&& map[i][j + 2] == 0 && sequence3_0 == false) {
						j = j + 2;
						new_map[i][count] = 0;
						sequence3_0 = true;
					} else if (map[i][j] == 1) {
						sequence3_0 = false;
						new_map[i][count] = 1;
					} else if (map[i][j] == 0 && map[i][j + 1] == 3
							&& map[i][j + 2] == 0) {
						j = j + 2;
						new_map[i][count] = 3;
					} else if (map[i][j] == 0 && map[i][j + 1] == 5
							&& map[i][j + 2] == 0) {
						j = j + 2;
						new_map[i][count] = 5;
					} else if (map[i][j] == 0 && sequence3_0 == true) {
						sequence3_0 = false;
						new_map[i][count] = 0;
					}
					count++;
				} else {
					if (map[i][j] == 5){
						new_map[i][count] = 5;
					}else if (map[i][j] == 3){
						new_map[i][count] = 3;
					}
				}
			}
			new_map[i][new_dimy-1] = 9;
		}
		
		mapDimension.dimX = mapDimension.dimX;
		mapDimension.dimY = new_dimy;
		
		//replaces the marker 9 by 1
		for(int i=0;i<mapDimension.dimX;i++)
			for(int j=0;j<mapDimension.dimY;j++)
				if(new_map[i][j] == 9)
					new_map[i][j] = 1;

		return new_map;

	}

	public int[][] createMap() {
		int[][] map = new int[mapDimension.dimX][mapDimension.dimY];
		int i = 0;
		try {
			scanner = new Scanner(file);
		} catch (FileNotFoundException exceptionFileNotFound) {
			exceptionFileNotFound.printStackTrace();
		}
		while (scanner.hasNextLine()) {
			StringBuffer line = new StringBuffer(scanner.nextLine());
			for (int j = 0; j < mapDimension.dimY; j++) {
				char tempChar = Character.toLowerCase(line.charAt(j));
				switch (tempChar) {
				case ' ':
					map[i][j] = 0;
					break;
				case 's':
					map[i][j] = 5;
					break;
				case 'e':
					map[i][j] = 3;
					break;
				case '+':
					map[i][j] = 9;
					break;
				default:
					map[i][j] = 1;
					break;
				}
			}
			i++;
		}
		scanner.close();
		return map;
	}

	class Dimension {
		int dimX;
		int dimY;

		public Dimension(int dimX, int dimY) {
			this.dimX = dimX;
			this.dimY = dimY;
		}
		
		public String toString(){
			return "Dimension: (" + dimX + ", " + dimY +")"; 
		}
	}
}
